package com.cqeec.demo.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.cqeec.demo.bean.AdminUser;
import com.cqeec.demo.mapper.AdminUserMapper;
import com.cqeec.demo.service.AdminUserService;

/**
 * 系统用户业务逻辑接口实现类
 * 
 * @Title: AdminUserServiceImpl
 * @Descripton: 定义系统用户业务逻辑接口实现
 */
@Service("adminUserService")
@Transactional(propagation=Propagation.REQUIRED, rollbackFor={RuntimeException.class})
public class AdminUserServiceImpl implements AdminUserService {
	
	@Resource
	private AdminUserMapper adminUserMapper;
	
	@Override
	@Transactional(propagation=Propagation.SUPPORTS, readOnly = true)
	public AdminUser login(String username, String password) {
		Map<String, Object> params = new HashMap<>();
		params.put("username", username);
		params.put("password", password);
		
		return adminUserMapper.login(params);
	}
	
	@Override
	@Transactional(propagation=Propagation.SUPPORTS, readOnly = true)
	public String getMysqlVersion()
	{
		return adminUserMapper.getMysqlVersion();
	}
	
	@Override
	@Transactional(propagation=Propagation.SUPPORTS, readOnly = true)
	public Map<String, Object> getUserListByPage(String username,int page,int pagesize)
	{
		Map<String, Object> params = new HashMap<>();
		params.put("username", username);
		int counts = adminUserMapper.recordCount(params);
		int pages = counts % pagesize == 0 ? counts / pagesize : counts / pagesize+1;
		if (page >= pages) {
			page = pages;
		}
		if (page <= 1) {
			page = 1;
		}
		params.put("page", (page - 1) * pagesize);
		params.put("pagesize", pagesize);
		List<AdminUser> user_list =  adminUserMapper.getUserListByPage(params);
		Map<String, Object> result = new HashMap<>();
		result.put("status",true);
		result.put("message","");
		result.put("total", counts);
		result.put("rows", user_list);
		return result;
	}
}
